class code2 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(p==root || q==root || root==null) return root;

        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        //两边都找到了
        if (left != null && right != null) {
            return root;
        }
        //只找到一边
        if (left != null && right == null) {
            return left;
        }
        if (right != null && left == null) {
            return right;
        }
        //两边都没找到
        return null;
    }
}